package com.controller;

import com.config.api.CommonResult;
import com.entity.News;
import com.entity.table.NewsTableDef;
import com.entity.table.UserTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.User;
import com.service.UserService;

import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author admin
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加。
     *
     * @param user 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody User user) {
//        return userService.save(user);
//    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return userService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param user 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
//    @PutMapping("update")
//    public boolean update(@RequestBody User user) {
//        return userService.updateById(user);
//    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
//    @GetMapping("list")
//    public List<User> list() {
//        return userService.list();
//    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public User getInfo(@PathVariable Serializable id) {
        return userService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<User> page(Page<User> page) {
        return userService.page(page);
    }

    /**
     * 添加商品类型表。
     *
     * @param user 商品类型表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(User user) {
        try {
            boolean r = userService.save(user);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = user.getId()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model){
        User user = userService.getById(id);
        model.addAttribute("user",user);
        return "back/user-edit";
    }
    @PostMapping("update")
    @ResponseBody
    public CommonResult update(User bType){
        try {
            boolean r = userService.updateById(bType);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String  msg = bType.getId()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @GetMapping(value = "/list")
    public String list(ModelMap model,
                       @RequestParam(value = "user",defaultValue = "") String userid,
                       @RequestParam(value = "p",defaultValue = "1") int p,
                       @RequestParam(value = "pageSize",defaultValue = "10")int paqeSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(UserTableDef.USER.ID.like(userid));
        Page<User> page = new Page<>();
        page.setPageSize(paqeSize);
        page.setPageNumber(p);
        Page<User> pageInfo = userService.page(page,queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "back/user-list";
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = userService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
    @GetMapping(value = "/addView")
    public String addUserView() { return "back/user-add"; }
}
